<template>
  <el-form class="mar-t-20 pad-r-60" ref="formModel" :model="formModel" label-width="80px">
    <el-form-item label="Where">
      <el-input @change="update()" v-model="formModel.where" placeholder=" dt=#YYYYMMdd# AND hour=#HH#"></el-input>
    </el-form-item>
    <el-form-item label="分区大小">
      <el-input-number @change="update()" class="w-49p" v-model="formModel.dataUnit.num" :min="0"></el-input-number>
      <el-select @change="update()" class="mar-l-5 w-49p" v-model="formModel.dataUnit.unit" placeholder="请选择">
        <el-option
          v-for="unit in ['day' , 'hour' , 'minute']"
          :key="unit"
          :label="unit"
          :value="unit">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="时区">
      <el-select class="dis-b" @change="update()" v-model="formModel.timeZone" placeholder="请选择">
        <el-option
          v-for="timezone in timezones"
          :key="timezone.name"
          :label="timezone.name"
          :value="timezone.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="完成文件">
      <div class="dis-b">
        <el-switch v-model="formModelPredicatesHas"></el-switch>
      </div>
      <div v-if="formModelPredicatesHas" class="dis-b">
        <el-input @change="update()" v-model="formModel.path" placeholder="/dt=#YYYYMMdd#/hour=#HH#/_DONE"></el-input>
      </div>
    </el-form-item>
  </el-form>
</template>
<script>
import { timezones } from './timezones';
export default {
  props: {
    model: {
      type: Object,
      default: null
    },
    location: {
      type: String,
      default: ''
    }
  },
  data(){
    return {
      timezones: timezones,
      formModelPredicatesHas: false,
      formModel: {
        where: '',
        timeZone: '',
        dataUnit: {
          num: 1,
          unit: 'day'
        },
        path: ''
      }
    }
  },
  mounted(){
    console.log(this.location , this.model);
    if(this.model){
      if(this.model['predicates']){
        this.formModelPredicatesHas = true;
        this.formModel.path = this.model['predicates'][0]['config']['path'];
      }
      this.formModel.where = this.model.where;
      this.formModel.timeZone = this.model['data.time.zone'];
      this.formModel.dataUnit = this.getTimeByUnit(this.model['data.unit']);
    }
    this.update();
  },
  methods: {
    getTimeByUnit(unit) {
      let regex = /^(\d+)([a-zA-Z]+)$/g;
      let arr = regex.exec(unit);
      if (arr.length > 2) {
        let n = parseInt(arr[1]);
        return {
          num: n,
          unit: arr[2]
        }
      } else {
        return {
          num: 1,
          unit: 'day'
        }
      }
    },
    update(){
      let model = {
       'data.unit': `${this.formModel.dataUnit.num}${this.formModel.dataUnit.unit}`,
       'data.time.zone': this.formModel.timeZone,
       'where': this.formModel.where
      } 
      if(this.formModelPredicatesHas && this.formModel.path){
        model['predicates'] = [{
          'type': 'file.exist',
          'config': {
            'path': this.formModel.path,
            'root.path': this.location
          }
        }]
      }
      this.$emit('save' , model);
    }
  }
}
</script>